#include "delay.h"

volatile uint32_t DELAY_UWTick;

void SysTick_Handler() // 滴答定时器中断服务函数
{
    DELAY_UWTick++;
}

void Delay_Init()
{
    SystemCoreClockUpdate();
    SysTick_Config(SystemCoreClock / 1000000 * RESOLUTION); // 开滴答定时器中断us级
}

uint32_t Get_Tick()
{
    return DELAY_UWTick * RESOLUTION;
}

void Delay_Us(uint32_t Us)
{
    uint32_t TickStart = Get_Tick();

    /* Add a period to guarantee minimum wait */
    if (Us < UINT32_MAX)
    {
        Us++;
    }

    while ((Get_Tick() - TickStart) < Us) // 等待时间到达
    {
    }
}

void Delay_Ms(uint16_t Ms)
{
    while (Ms-- != 0)
    {
        Delay_Us(1000); // 调用1000微秒的延时
    }
}

void Delay_S(uint16_t S)
{
    while (S-- != 0)
    {
        Delay_Ms(1000); // 调用1000毫秒的延时
    }
}
